/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.icci.explorer;



import idea.model.qnrlibrary.CustomContentItems;
import java.awt.BorderLayout;
import java.util.List;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.swing.outline.Outline;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;


/**
 * Top component which displays Custom Content Items.
 * @author rejun001
 * @see TopComponent
 * @see ExplorerManager.Provider
 */
@ConvertAsProperties(
dtd = "-//idea.icci.explorer//CustomContentItems//EN" ,
autostore = false )
@TopComponent.Description(
preferredID = "CustomContentItemsTopComponent" ,
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( position = 5, mode = "editor" , openAtStartup = false )
@ActionID( category = "Window" , id = "idea.icci.explorer.CustomContentItemsTopComponent" )
@ActionReferences({
        @ActionReference( path = "Menu/Window" , position = 633  ),
        @ActionReference( path = "Menu/File/Open" , position = 1500 )
})
@TopComponent.OpenActionRegistration(
displayName = "#CTL_CustomContentItemsAction" ,
preferredID = "CustomContentItemsTopComponent" )
@Messages(
{
    "CTL_CustomContentItemsAction=Custom Content Items" ,
    "CTL_CustomContentItemsTopComponent=Custom Content Items Window" ,
    "HINT_CustomContentItemsTopComponent=This is a Custom Content Items window"
} )
public final class CustomContentItemsTopComponent extends TopComponent implements ExplorerManager.Provider
{

    /**
     *
     */
    private static ExplorerManager exMgr = new ExplorerManager();

    /**
     * xtor
     */
    public CustomContentItemsTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_CustomContentItemsTopComponent() );
        setToolTipText( Bundle.HINT_CustomContentItemsTopComponent() );
        setLayout( new BorderLayout() );
        
        putClientProperty("print.printable", Boolean.TRUE);
        putClientProperty("print.name", "Ed's Custom Content Items");

        associateLookup( ExplorerUtils.createLookup( exMgr , getActionMap() ) );
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        lblCCI = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        beanTreeView1 = new org.openide.explorer.view.BeanTreeView();

        lblCCI.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblCCI, org.openide.util.NbBundle.getMessage(CustomContentItemsTopComponent.class, "CustomContentItemsTopComponent.lblCCI.text")); // NOI18N

        jScrollPane1.setViewportView(beanTreeView1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCCI)
                .addContainerGap(251, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
                    .addGap(12, 12, 12)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblCCI)
                .addGap(0, 610, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(25, 25, 25)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                    .addGap(26, 26, 26)))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCCI;
    // End of variables declaration//GEN-END:variables


    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }


    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }


    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }


    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }

    /**
     *
     */
    @Override
    public ExplorerManager getExplorerManager()
    {
        return exMgr;
    }


    /**
     * Display the list of CustomContent Items for the given Question Title.
     * @param resultList - the Custom Content Items
     * @param seq -- for the label
     * @param category -- for the label
     */
    public void displayCCIs( List<CustomContentItems> resultList, String seq, String category )
    {
        ExplorerManager exm = getExplorerManager();
        Children kids = Children.create( new ICustomContentItemChildFactory( resultList ) , true );
        Node rootNode = new ICustomContentItemRootNode( kids );

        OutlineView ov = new OutlineView( "Sequence" );
        ov.setPropertyColumns(
                //"itemSequence", "Sequence",
                "itemNumber" , "Item Number" ,
                "itemWording" , "Item Wording" );
        
        Outline o = ov.getOutline();
        o.setRootVisible(false);
        //o.getColumnModel().getColumn( 0).setMaxWidth(30);
        //o.getColumnModel().getColumn( 0).setResizable( false);

        add(ov, BorderLayout.CENTER);
        int size = resultList.size();
        String res = (size == 1 ? size + " result" : size + " results");
        lblCCI.setText( org.openide.util.NbBundle.getMessage(CustomContentItemsTopComponent.class, "CustomContentItemsTopComponent.lblCCI.text") 
                + " (for Question Title " + seq + ": \"" + category + "\") -- " + res);
        add(lblCCI, BorderLayout.NORTH);
        exm.setRootContext( rootNode );
    }
}
